import store from '../store';
import {
	checkLogin
} from './login';
import Cache from '../utils/cache';
import {
	STATE_R_KEY,
	USER_INFO,
	OPENID,
	PATIENT_IFNO,
	PATIENT_LIST
} from './../config/cache';
import * as AuthApi from "@/api/member/auth";
import * as Api from '@/api/api.js';
import * as config from "@/api/config.js";
import * as PatientApi from '@/api/hospital/patient.js';



/**
 * 获取用户档案
 *
 * @param openid openid
 */
export function getUserPatient(openid) {
	console.info("getUserPatient openid:" + openid);
	const queryData = new Object();
	queryData.userId = openid;
	
	// if (patientInfoStr) {
		// let patientInfo = JSON.parse(patientInfoStr);
		// if (!patientInfo.id) {
			return new Promise((resolve, reject) => {
				PatientApi.list(queryData).then(res => {
					console.info("PatientApi.list res");
					console.table(res);
					if (res.code !== 0) {
						store.commit("UPDATE_PATIENTINFO", {});
						store.commit("UPDATE_PATIENTLIST", []);
						resolve(res);
					}
					
					let array = new Array();
					let patientInfoStr = Cache.getEncrypt(PATIENT_IFNO);
					if(patientInfoStr){
						// let patient = JSON.parse(patientInfoStr);
						let patient = patientInfoStr;
						if(patient.patientId){
							array = res.data.filter((val) => (val.patientId === patient.patientId));
						}
					}
					// console.table(array);
					if(array.length > 0){
						store.commit("UPDATE_PATIENTINFO", array[0]);
					}else{
						let index = res.data.length - 1;
						store.commit("UPDATE_PATIENTINFO", res.data[index]);
					}

					store.commit("UPDATE_PATIENTLIST", res.data);
					return resolve(res);
				}).catch(res => {
					return reject(res);
				})
			})
		// }
	// }

}

export function veifyUserPatient(openid,id,registerOrderId,verifyOrderId) {
	const queryData = new Object();
	queryData.id = id;
	queryData.registerOrderId = registerOrderId;
	queryData.verifyOrderId = verifyOrderId;
	PatientApi.checkUniformVerifyResult(queryData).then(res => {
		console.info("checkUniformVerifyResult res");
		console.table(res);
		if (res.code === 0) {
			if (res.data.suc) {
				getUserPatient(openid)
			}
		}
	}).catch(res => {
		return reject(res);
	})
}